package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-10 11:03
 **/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 条件查询 + 分页查询
     *
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> queryStudentList(StudentQueryParam studentQueryParam) {
        //1,获取分页参数
        Page<Student> page = new Page<>(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        //2,设置分页查询参数
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(studentQueryParam.getName()), Student::getName, studentQueryParam.getName());
        queryWrapper.eq(studentQueryParam.getDegree() != null, Student::getGender, studentQueryParam.getDegree());
        queryWrapper.eq(studentQueryParam.getClazzId() != null, Student::getClazzId, studentQueryParam.getClazzId());
        //执行查询
        page = this.page(page, queryWrapper);
        long total = page.getTotal();
        List<Student> records = page.getRecords();
        return new PageResult<>(total, records);
//        // 1.设置分页参数
//        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
//        // 2.执行查询
//        List<Student> list = studentMapper.queryStudentList(studentQueryParam);
//        // 3.封装
//        PageInfo<Student>pageInfo = new PageInfo<>(list);
//        //4.返回
//        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    @Override
    public Integer addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        Integer num = studentMapper.insert(student);
        return num;
    }

    /**
     * 根据id查询学生
     *
     * @param id
     * @return
     */
    @Override
    public Student queryStudentById(Integer id) {
        return studentMapper.selectById(id);
    }

    /**
     * 修改学生
     *
     * @param student
     * @return
     */
    @Override
    public Integer updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateById(student);
    }

    /**
     * 批量删除学生
     *
     * @param ids
     * @return
     */
    @Override
    public Integer deleteStudentByIds(List<Integer> ids) {
        Integer integer = studentMapper.deleteByIds(ids);
        return integer;
    }

    /**
     * 学生违纪扣分
     *
     * @param id
     * @param score
     */
    @Override
    public void updateViolationStu(Integer id, Short score) {
        Student student = studentMapper.selectById(id);
        LocalDateTime updateTime = LocalDateTime.now();
        // 得到原来的违纪得分
        int violationScore = student.getViolationScore();
        int violationCount = student.getViolationCount();
        violationScore += score;
        violationCount++;
        studentMapper.updateViolationStu(id, violationScore, violationCount, updateTime);

    }
}
